package com.budwk.app.sqfwDoc.codeGeneratorV2.pdfService;

import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.capacity.AbstractCreatePdfCapacity;
import com.budwk.app.sqfwDoc.codeGeneratorV2.capacity.IAfterCreatePdfCapacity;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonPdfParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfPdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.result.GeneratorPdfWordResult;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUnitService;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ZY.Wang
 * @since 2024/1/24
 */
public abstract class CommonPdfService extends AbstractCreatePdfCapacity implements IAfterCreatePdfCapacity {

    ExecutorService executorService = Executors.newFixedThreadPool(20);

    public void createPdfAndMsg(CommonPdfParamForController param) {
        String userId = param.getUserId();
        Map<String, Object> businessData = param.getCustomerParamMap();
        Map<String, Object> businessMap = saveBusinessData(userId, businessData);
        executorService.execute(() -> {
            PdfGenerateParam pdfGenerateParam = createPdfParam(businessMap);
            GeneratorPdfWordResult generatorPdfWordResult = generatePdf(pdfGenerateParam);
            afterGeneratePdf(generatorPdfWordResult);
        });
    }

    public void reCreatePdfAndMsg(CommonPdfParamForController param) {

    }

    public GeneratorPdfWordResult createPdfSync(CommonPdfParamForController param) {
        String userId = param.getUserId();
        Map<String, Object> businessData = param.getCustomerParamMap();
        Map<String, Object> businessMap = saveBusinessData(userId, businessData);
        PdfGenerateParam pdfGenerateParam = createPdfParam(businessMap);
        GeneratorPdfWordResult generatorPdfWordResult = generatePdf(pdfGenerateParam);
        afterGeneratePdf(generatorPdfWordResult);
        return generatorPdfWordResult;
    }

    protected CommonParam getCreateUserPdfParam(String createBy) {
        CommonParam commonParam = new CommonParam();
        SysUserService sysUserService = NutzIocUtils.getBean(SysUserService.class);
        Sys_user createUser = sysUserService.fetch(createBy);
        commonParam.setCreateUserId(createUser.getId());
        commonParam.setCreateUserName(createUser.getUsername());
        commonParam.setCreateUserPhone(createUser.getLoginname());
        commonParam.setUnitId(createUser.getUnitId());
        commonParam.setLevelCityUnitName(createUser.getUnitName());
        commonParam.setProvince(createUser.getProvince());
        commonParam.setCity(createUser.getCity());
        commonParam.setDistrict(createUser.getDistrict());
        commonParam.setCounty(createUser.getCounty());
        commonParam.setCommunity(createUser.getCommunity());
        commonParam.setCreateUserSigUrl(createUser.getSignatureUrl());

        SysUnitService sysUnitService = NutzIocUtils.getBean(SysUnitService.class);
        Sys_unit userUnit = sysUnitService.fetch(createUser.getUnitId());
        commonParam.setUnitName(userUnit.getName());
        return commonParam;
    }

    abstract public Map<String, Object> saveBusinessData(String userId, Object data);

    abstract protected PdfGenerateParam createPdfParam(Map<String, Object> businessMap);

    @Override
    abstract protected void customerPopularPdfGenerateParam(PdfGenerateParam pdfGenerateParam);

    @Override
    abstract protected List<BasePdfGenerator> getPdfGenerator(PdfGenerateParam pdfGenerateParam);

    @Override
    abstract protected String getDownloadFileName(PdfGenerateParam pdfGenerateParam);

    @Override
    protected void popularPdfGenerateResultBridgeMap(GeneratorPdfWordResult result, List<ResultOfPdfGenerator> pdfResultList) {

    }

    abstract protected AbstractCustomerPdfCreateField getCustomerPdfField(String fieldStr);

    @Override
    public void afterGeneratePdf(GeneratorPdfWordResult generateResult) {

    }
}
